/**    
 * 文件名：JavaCvSift.java    
 *    
 * 版本信息：    
 * 日期：2014年3月11日    
 * xyj 足下 xyj 2014     
 * 版权所有    
 *    
 */

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvConvert;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static opencvtest.OpenCVUtils.loadAndShowOrExit;
import static opencvtest.OpenCVUtils.toIplImage8U;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_features2d.BOWKMeansTrainer;
import com.googlecode.javacv.cpp.opencv_features2d.DescriptorExtractor;
import com.googlecode.javacv.cpp.opencv_features2d.KeyPoint;
import com.googlecode.javacv.cpp.opencv_nonfree.SIFT;

/**
 * @项目名称：opencv-test
 * @类名称：JavaCvSift
 * @类描述：
 * @创建人：zhuyi
 * @创建时间：2014年3月11日 下午6:14:35
 * @修改人：zhuyi
 * @修改时间：2014年3月11日 下午6:14:35
 * @修改备注：
 * @version
 * 
 */
public class JavaCvSift {

    public static void main(String[] args) {

        List<CvMat> descriptorsArray = new ArrayList<CvMat>();

        for (int j = 100; j <= 100; j++) {
            IplImage image = loadAndShowOrExit(new File("E:/ii/" + j + ".jpg"), CV_LOAD_IMAGE_COLOR);

            System.out.println(image.depth());
            if (image.depth() != IPL_DEPTH_8U) {
                image = toIplImage8U(image, true);
            }

            KeyPoint keyPoints = new KeyPoint();
            int nFeatures = 0;
            int nOctaveLayers = 3;
            float contrastThreshold = 0.03f;
            int edgeThreshold = 10;
            float sigma = 1.6f;
            SIFT sift = new SIFT(nFeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma);
            DescriptorExtractor siftDesc = DescriptorExtractor.create("SIFT");
            sift.detect(image, null, keyPoints);
            CvMat descriptors = new CvMat(null);
            siftDesc.compute(image, keyPoints, descriptors);
            descriptorsArray.add(descriptors);
        }

        // IplImage featureImage = IplImage.create(cvGetSize(image),
        // image.depth(), 3);
        // drawKeypoints(image, keyPoints, featureImage, CvScalar.RED,
        // DrawMatchesFlags.DRAW_RICH_KEYPOINTS);
        // show(featureImage, "SIFT Features");

        CvMat trainingDescriptors = CvMat.create(1, descriptorsArray.size());
        for (int i = 0; i < descriptorsArray.size(); i++) {
            CvMat srcDescriptor = descriptorsArray.get(i);

            trainingDescriptors.put(srcDescriptor);
        }

        CvMat descriptor = CvMat.create(trainingDescriptors.rows(), trainingDescriptors.cols(), CV_32F,
                trainingDescriptors.channels());
        cvConvert(trainingDescriptors, descriptor);

        BOWKMeansTrainer trainer = new BOWKMeansTrainer(1);
        trainer.add(trainingDescriptors);
        CvMat vocabulary = trainer.cluster();
        System.out.println(vocabulary);
    }
}
